package com.wzq.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzq.dao.pojo.Member;
import com.wzq.dao.pojo.PageBean;
import com.wzq.dao.pojo.R;
import com.wzq.service.IMemberService;
import com.wzq.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/member")
public class AdminMemberController extends  GetSome{

    @Autowired
    IMemberService iMemberService;
    @RequestMapping("/list")
    public R list(@RequestBody PageBean pageBean){
        System.out.println(pageBean);
        String query=pageBean.getQuery().trim();
        Page<Member> page=new Page<>(pageBean.getPageNum(),pageBean.getPageSize());
        Page<Member> pageResult = iMemberService.page(page, new QueryWrapper<Member>().in(StringUtil.isNotEmpty(query), "w_id", searchByWname(query)));
        //        添加用户名称和项目名称
        List<Member> list=new ArrayList<>();
        for (Member member : pageResult.getRecords()) {
            if (member.getIsaccpt() == 1)        member.setIsaccpt_c("接受");
            else                                 member.setIsaccpt_c("未接受");
            if (member.getIdentities() == 0)        member.setIdentities_c("创建者");
            else                                 member.setIdentities_c("成员");
            member.setW_name( getW_name(member.getW_id()) );
            member.setP_name( getP_name(member.getP_id()) );
            list.add(member);
        }
        Map<String,Object> map=new HashMap<>();
        map.put("MemberList",pageResult.getRecords());
        map.put("total",pageResult.getTotal());

        return R.ok(map);
    }

    @RequestMapping("/save")
    public R save(@RequestBody Member member){
        if(member.getId()==null || member.getId()==-1){
            iMemberService.save(member);
        }else{
            iMemberService.saveOrUpdate(member);
        }
        return R.ok();
    }

    @GetMapping("/{id}")
    public R findById(@PathVariable(value = "id")Integer id){
        Member member = iMemberService.getById(id);
        //        获得用户名称和项目名称
        member.setW_name( getW_name(member.getW_id()) );
        member.setP_name( getP_name(member.getP_id()) );
        Map<String,Object> map=new HashMap<>();
        map.put("member",member);
        return R.ok(map);
    }

    @GetMapping("/delete/{id}")
    public R delete(@PathVariable(value = "id")Integer id){
        iMemberService.removeById(id);
        return R.ok();
    }
}

